﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion


namespace ESF.General.Commands
{
	public class ConcreteCommand<O> : ICommand <O>
	{
		#region Delegados

		public delegate O QuickExecute (O pLastStateObject, ref Dictionary <string, object> pParameters);
		
		#endregion


		#region Miembros

		private O _CurrentObject;

		private Dictionary <string, object> _Parameters;

		private QuickExecute _QuickExecuteDefinition;

		#endregion


		#region Properties

		public O CurrentObject
		{
			get
			{
				return _CurrentObject;
			}
		}


		public Dictionary <string, object> Parameters
		{
			get
			{
				return _Parameters;
			}
		}

		#endregion


		#region Constructor

		public ConcreteCommand ()
		{	
			_Parameters = new Dictionary<string,object> ();
		}


		public ConcreteCommand (QuickExecute pQuickExecuteDefinition) : this ()
		{
			_QuickExecuteDefinition = pQuickExecuteDefinition;
		}

		#endregion


		#region Funciones

		public void Execute (O pLastObjectState)
		{
			Execute (pLastObjectState, null);
		}


		public void Execute (O pLastObjectState, Dictionary <string, object> pParameters)
		{
			_CurrentObject = ObjectHelper.Clone<O> (pLastObjectState);

			if (pParameters != null)
			{
				_Parameters = pParameters;
			}

			Execute ();
		}


		protected virtual void Execute ()
		{
			if (_QuickExecuteDefinition != null)
			{
				_CurrentObject = _QuickExecuteDefinition (_CurrentObject, ref _Parameters);
			}
		}

		#endregion
	}
}
